home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / server / src / exit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  6.5 KB  |  309 lines

  1. #include "headers.h"
  2.  
  3. /* this file has various functions for exiting and aborting */
  4.  
  5. /* free structures and exit */
  6. void quit(int stat)
  7. {
  8.    int res;
  9.    int jumped;    
  10.  
  11.    jumped = setjmp(doquit);
  12.    if ((jumped == ERROR) && (errno != 0)) 
  13.       error("error with setjmp: %s\n\n", strerror(errno));
  14.  
  15.    /* just for setjmp()..not to quit */
  16.    if (stat == INIT) return; 
  17.  
  18.    signal(SIGINT,  SIG_IGN);
  19.    signal(SIGTERM, SIG_IGN);
  20.  
  21.    signal(SIGPIPE, SIG_IGN);
  22.  
  23.    signal(SIGUSR1, SIG_IGN);
  24.    signal(SIGUSR2, SIG_IGN);
  25.    
  26.    if (debugging != 1)
  27.    {
  28.       signal(SIGILL,  SIG_IGN);
  29.       signal(SIGBUS,  SIG_IGN);
  30.       signal(SIGSEGV, SIG_IGN);
  31.       signal(SIGQUIT, SIG_IGN);
  32.    }
  33.  
  34. /* ------------------------------------ */
  35.  
  36.    /* this is where all the closing/killing/freeing takes place */
  37.     
  38.    error("received abort request...\n\n");
  39.  
  40.    if (mainpid <= 0)
  41.    {
  42.       error("main pid unknown.. aborting immediately\n\n");
  43.       exit(ERROR);
  44.    }
  45.  
  46.    if (mainpid != getpid())
  47.    {
  48.       (void)putchar('\n');
  49.       (void)write(dblogfd, "\n", 1);
  50.    }
  51.  
  52.    closeSockets(); /* close open sockets */
  53.  
  54.    if (mainpid == getpid())
  55.       if (mainfd > 0) (void)close(mainfd);
  56.  
  57.    if (mainpid != getpid()) closeLogFiles();
  58.  
  59.    freeSharedMem();
  60.    /* freeClients(); - we're not using dynamic memory right now */
  61.  
  62.    res = seteuid(0); 
  63.    if (res == ERROR)
  64.    { 
  65.       error("error with seteuid: %s\n\n", strerror(errno));
  66.       quit(ERROR);
  67.    }
  68.  
  69.    if (mainpid == getpid()) doKills();
  70.    else if (clients[curClient].pidstats[0].pid == getpid()) doKills();
  71.  
  72.    if (pwd != NULL)
  73.    {
  74.       res = seteuid(pwd->pw_uid);
  75.  
  76.       if (res == ERROR)
  77.       { 
  78.          error("error with seteuid: %s\n\n", strerror(errno));
  79.          quit(ERROR);
  80.       }
  81.    }
  82.  
  83.    if (mainpid == getpid())
  84.    {
  85.       (void)close(errlogfd), (void)fclose(errlogfd1);
  86.  
  87.       if (debugging == 1)
  88.       {
  89.          (void)close(dblogfd);
  90.          (void)fclose(dblogfd1);
  91.       }
  92.    }
  93.  
  94.    else
  95.    {
  96.       if (debugging == 1)
  97.       {
  98.          (void)close(dblogfd);
  99.          (void)fclose(dblogfd1);
  100.       }
  101.    }
  102.  
  103.    if ((debugging == 1) && (mainpid == getpid()))
  104.    {
  105.       (void)printf("\nfinished closing..\n");
  106.       (void)printf("see error.log for a log\n\n");
  107.    }
  108.  
  109.    /* jumped == value passed by longjmp() */
  110.    if (jumped == 0) exit(stat);
  111.    else exit(jumped);
  112. }
  113.  
  114.  
  115. /* ------------------------------------ */
  116.  
  117.  
  118. /* close all open sockets  */
  119. void closeSockets()
  120. {
  121.    int res;
  122.    register int i;
  123.  
  124.    debug("closing sockets\n");
  125.    if (mainpid != getpid()) 
  126.    {
  127.       if (clients[curClient].sockfd > 0) 
  128.          res = close(clients[curClient].sockfd);
  129.  
  130.       if (res == ERROR)
  131.       {
  132.          error("error closing sockfd: %s\n\n", strerror(errno));
  133.          if (debugging == 1)
  134.          {
  135.             (void)putchar('\n');
  136.             (void)write(dblogfd, "\n", 1);
  137.          }
  138.       }
  139.  
  140.       return;
  141.    }
  142.  
  143.    for (i = 0; i < MAXCONNS; i++)
  144.    {
  145.       if ((clients[i].free == 1) || (clients[i].sockfd <= 0)) continue;
  146.  
  147.       res = close(clients[i].sockfd);
  148.       if (res == ERROR) 
  149.          error("error closing client's fd: %s\n\n", strerror(errno));
  150.    }
  151. }
  152.  
  153.  
  154. /* ------------------------------------ */
  155.  
  156.  
  157. /* close and free logs */
  158. void closeLogFiles()
  159. {
  160.    register int i;
  161.  
  162.    debug("closing log files\n");
  163.  
  164.    for (i = 0; i <= curlogfd; i++)
  165.    {
  166.       if (clients[curClient].logs[i].fd > 0)
  167.          (void)close(clients[curClient].logs[i].fd);
  168.  
  169.       /* okay.. so technically it shouldn't be in closeSockets() */
  170.       /* but when we play with logs[].fd.. this goes too         */
  171.       if (clients[curClient].logs[i].filename != NULL) 
  172.          free(clients[curClient].logs[i].filename);
  173.    }
  174. }
  175.  
  176.  
  177. /* ------------------------------------ */
  178.  
  179.  
  180. /* free shared memory, remove shared mem IDs, detach segments, etc. */
  181. void freeSharedMem()
  182. {
  183.    int res;
  184.    register int i = 0;
  185.  
  186.    if (mainpid != getpid())
  187.    {
  188.       debug("detaching shared memory\n");
  189.       res = shmdt(clients[i].segptr);
  190.  
  191.       if (res == ERROR)
  192.       {
  193.          error("error detaching shared memory: %s\n\n", strerror(errno));
  194.          if (debugging == 1)
  195.          {
  196.             (void)putchar('\n');
  197.             (void)write(dblogfd, "\n", 1);
  198.          }
  199.       }
  200.  
  201.       return;
  202.    }
  203.  
  204.    debug("detaching shared memory\n");
  205.  
  206.    for (i = 0; clients[i].segptr != NULL; i++)
  207.    {
  208.       res = shmdt(clients[i].segptr);
  209.       if (res == ERROR)
  210.          error("error detaching shared memory: %s\n\n", strerror(errno));
  211.    }
  212. }
  213.  
  214.  
  215. /* ------------------------------------ */
  216.  
  217.  
  218. /* free up client structures */
  219. void freeClients()
  220. {
  221. /* 
  222.    register int i = 0;
  223.  
  224.    debug("freeing client structures\n");
  225.  
  226.    for (i = 0; i < MAXCONNS; i++)
  227.       if (clients[i] != NULL)
  228.          free(clients[i]);
  229. */
  230. }
  231.  
  232.  
  233. /* ------------------------------------ */
  234.  
  235.  
  236. /* kill all child processes */
  237. void doKills()
  238. {
  239.    int res;
  240.    int didkill = 0;
  241.  
  242.    register int i = 0;
  243.  
  244.    if (mainpid != getpid())
  245.    {
  246.       if (clients[curClient].pinging == 1)
  247.       {
  248.          if (debugging == 1)
  249.          {
  250.             (void)putchar('\n'); 
  251.             (void)write(dblogfd, "\n", 1);
  252.          }
  253.  
  254.          debug("clients[%d].pinging = 1..\n", curClient);
  255.  
  256.          if (clients[curClient].pidstats[0].pid <= 0)
  257.          {
  258.             error("(in doKills) pidstats[0].pid <= 0..\n\n");
  259.  
  260.             if ((clients[curClient].pidstats[1].pid > 0) && 
  261.                 (clients[curClient].pidstats[1].pid == getpid()))
  262.                clients[curClient].pidstats[0].pid = getpid();
  263.  
  264.             else return;
  265.          }
  266.  
  267.          if (clients[curClient].pidstats[0].pid == getpid())
  268.          {
  269.             if (clients[curClient].pidstats[1].pid <= 0) return;
  270.  
  271.             debug("killing ping process (pid %d)\n",
  272.                   clients[curClient].pidstats[1].pid);
  273.  
  274.             res = kill(clients[curClient].pidstats[1].pid, SIGTERM);
  275.             if (res == ERROR)
  276.                error("error killing ping process: %s\n\n", strerror(errno));
  277.  
  278.             didkill = 1;
  279.          }
  280.  
  281.          if ((didkill == 1) && (debugging == 1))
  282.          {
  283.             (void)putchar('\n');
  284.             (void)write(dblogfd, "\n", 1);
  285.          }
  286.       }
  287.  
  288.       return;      
  289.    }
  290.  
  291.    /* kill all client handlers (but not the main superserver.. that's us) */
  292.    for (i = 0; i < MAXCONNS; i++)
  293.    {
  294.       if (clients[i].free == 1) continue;
  295.       else if (clients[i].pidstats[0].pid <= 0) continue;
  296.       else if (clients[i].pidstats[0].pid != getpid())
  297.       {
  298.          debug("killing the client hander (pid %d)... ID %04d\n",
  299.                clients[i].pidstats[0].pid, clients[i].ID);
  300.  
  301.          (void)kill(clients[i].pidstats[0].pid, SIGTERM);
  302.          didkill = 1;
  303.       }
  304.    }
  305. }
  306.  
  307.  
  308. /* ------------------------------------ */
  309.